package ogb;

import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.BulletState;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests the accuracy of fire of DeaconBlues.
 * 
 * @author Branden Ogata
 *
 */
public class TestDeaconBluesFire extends RobotTestBed {
  private int bulletCount = 0;
  private int bulletHit = 0;
  List<Integer> shots = new ArrayList<Integer>();
  List<Integer> hits = new ArrayList<Integer>();
  
  /**
   * Specifies SittingDuck and DeaconBlues as the robots involved in this test.
   * 
   * @return  The names of the robots in this match.
   * 
   */
  
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,ogb.DeaconBlues";
  }

  /**
   * Returns the number of rounds in this match.
   * 
   * @return  The number of rounds in this test.
   * 
   */
  
  @Override
  public int getNumRounds() {
    return 8;
  }
  
  /**
   * Every turn, determine the accuracy of the firing strategy of DeaconBlues.
   * (This strategy is nonexistent, but nonetheless.)
   * 
   * @param e The event that the game gives.
   */
  
  @Override
  public void onTurnEnded(TurnEndedEvent e) {
    IBulletSnapshot bullets[] = e.getTurnSnapshot().getBullets();
    
    // For all the bullets on the field
    for (int i = 0; i < bullets.length; i++) {
      // If the bullets are not already recorded 
      if (!shots.contains(bullets[i].getBulletId())) {
        bulletCount++;
        shots.add(bullets[i].getBulletId());
      }
      
      // If the bullet has hit a target, then increment the number of hits
      // and add the bullet to the list of bullets that hit
      if ((bullets[i].getState() == BulletState.HIT_VICTIM) && 
          (!hits.contains(bullets[i].getBulletId()))) {
        bulletHit++;
        hits.add(bullets[i].getBulletId());
      }
    }
  }
  
  /**
   * At the end of the battle, find the proportion of hits to bullets fired.
   * 
   * @param e The event that the game gives.
   * 
   */
  
  @Override
  public void onBattleCompleted(BattleCompletedEvent e) {
    assertTrue("Check aim with " + bulletHit + " hits out of " + bulletCount + " bullets ", 
              (((double) bulletHit / bulletCount) > 0.50));
  }
}
